package com.tsxy.mall.controller;

import com.tsxy.mall.entity.Cart;
import com.tsxy.mall.entity.User;
import com.tsxy.mall.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user/cart")
public class CartController {
    @Autowired
    private CartService cartService;

    @GetMapping
    public String cart() {
        return "user/cart";
    }

    @GetMapping("/add")
    public String add (Integer id, HttpSession session){
        User user = (User)session.getAttribute("user");
        Cart cart = new Cart();
        cart.setUserId(user.getId());
        cart.setBookId(id);
        cart.setCount(1);
        cart.setBookType("0");
        cartService.insert(cart);
        return "redirect:/user/cart";
    }
    @GetMapping("/addUsedBook")
    public String addUsedBook (Integer id, HttpSession session){
        User user = (User)session.getAttribute("user");
        Cart cart = new Cart();
        cart.setUserId(user.getId());
        cart.setBookId(id);
        cart.setCount(1);
        cart.setBookType("1");
        cartService.insert(cart);
        return "redirect:/user/cart";
    }
}
